package com.my.interview.google.guava;

import com.google.common.base.Ticker;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class GuavaCacheStudy {


    public static LoadingCache<String, String> loadingCache = CacheBuilder.newBuilder()
            .expireAfterWrite(10, TimeUnit.SECONDS)
            .ticker(Ticker.systemTicker())
            .build(new CacheLoader<String, String>() {
                @Override
                public String load(String s) throws Exception {
                    return s;
                }
            });


    public static void main(String[] args) {

        loadingCache.put("name", "password");
        try{
            System.out.println(loadingCache.get("name"));
        }catch (ExecutionException e){
            e.printStackTrace();
        }

        loadingCache.put("password", "123456");

        ConcurrentMap<String, String> concurrentMap = loadingCache.asMap();
        System.out.println(concurrentMap.get("name"));

        loadingCache.put("gender", "male");
        System.out.println(concurrentMap.get("gender"));
        concurrentMap.put("gender", "female");
        System.out.println(concurrentMap.get("gender"));


    }
}
